<?php
define('PATH', dirname(dirname(__FILE__)));
require_once PATH . '/vendor/autoload.php';

use DiDom\Document;
use DiDom\Query;
use Carbon\Carbon;

/**
 * 天气后报网的日期格式转换
 * eg: 2011年01月01日--- 2011-01-01
 */
function formatDate ($dateStr)
{
    preg_match_all('/\d+/', $dateStr, $dateArray);
    $formatStr = implode('-', $dateArray[0]);
    echo $formatStr, PHP_EOL;
}

/**
 * 拼凑年月格式的数据，如201201
 */
function generateUrl($startFrom = 2016)
{
    $nowYear = date('Y');
    $month = range(1, 12);
    $arr = [];
    for ($i = $startFrom; $i <= $nowYear-1; $i++) { //16年的数据还没有
        foreach($month as $m) {
            if ($i == $nowYear && $m > date('m')) {
                break;
            }
            $arr[] = $i . sprintf("%'02d", $m);
        }
    }
    return $arr;
}

/**
 * 获取网页内容
 */
function getContents($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
    
}

function save($name, $data)
{
    if ($name === '' || $data == null) {
        return;
    }
    $file = fopen(PATH . '/data/' . $name . '.dat', 'a+') or die('无法打开文件');
    if (is_array($data)) {
        $data = implode("\n", $data);
//        foreach ($data as $line) {
//            fwrite($file, $line . "\n");
//        }
    }
    fwrite($file, $data . "\n");
    fclose($file);
}

/**
 * 数据处理的方法
 * 日期,最高气温,最低气温,天气,风向,风力,星期,季节
 */
function handleData($data) {
    array_push($data, Carbon::parse($data[0])->dayOfWeek);
    $data = implode(',', $data);
    return $data;
}


$document = new Document();
$doc = new Document();
$monthArr = generateUrl(2015);
foreach ($monthArr as $m) {
    $fileContent = [];
    $content = getContents("http://lishi.tianqi.com/huadu/" . $m . '.html');
    $document->loadHtml($content);
    $data = $document->find("div.tqtongji2>ul", Query::TYPE_CSS);
    $counts = count($data);
    for ($i = 1; $i < $counts; $i++) {
        $html = $data[$i]->html();
        $d = $doc->loadHtml($html)->find('li');
        foreach ($d as $ele) {
            $lineArr[] = $ele->text();
        }
        //$fileContent[] = implode(',', $lineArr);
        $fileContent[] = handleData($lineArr);
        $lineArr = [];
    }
    save($m, $fileContent);
    $fileContent = [];
}
echo 'finished', PHP_EOL;
exit;
